package com.izerui.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import nc.vo.framework.rsa.Encode;

import org.apache.commons.collections.MapUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.izerui.mapper.PortalMapper;
import com.izerui.mapper.ThamsMapper;
import com.izerui.service.PortalService;

@Service("portalService")
public class PortalServiceImpl implements PortalService {
	
	private static Logger log = Logger.getLogger(PortalServiceImpl.class);

	@Autowired
	private PortalMapper portalMapper;
	
	@Autowired
	private ThamsMapper thamsMapper;

	@Override
	public List<Map> getUserList() {
		return portalMapper.getAllUserList();
	}

	@Override
	public void initData() {
		List<Map> corporationList = portalMapper.getCorporationList();
		for (Map map : corporationList) {
			String corporationName = MapUtils.getString(map, "DEPTNAME");//公司名称
			//如果不存在该全宗
			String qzh = fillZero(MapUtils.getString(map, "DEPTCODE"));
			Map thamsQz = thamsMapper.getQzByQzh(qzh);
			map.put("QZH", qzh);
			if(thamsQz==null){
				map.put("DID", thamsMapper.getMaxDIDForQzh()+1);
				map.put("QZMC", corporationName);
				map.put("ISDEF", 0);
				thamsMapper.insertQzh(map);
				log.info("添加全宗: ===>"+corporationName);
			}else{//如果存在
				thamsMapper.updateQzmc(qzh, corporationName);
				log.info("全宗: ===>"+corporationName);
			}
			
			Map roleMap = thamsMapper.getRoleByJsmc(corporationName+"-普通用户");
			if(roleMap==null){
				//添加普通用户角色
				roleMap = new HashMap();
				Integer jsid = thamsMapper.getMaxDIDForRole();
				jsid = jsid!=null?jsid+1:1;
				roleMap.put("DID", jsid);
				roleMap.put("JSMC", corporationName+"-普通用户");
				roleMap.put("ISXTMR", 0);
				thamsMapper.insertRole(roleMap);
				log.info("添加默认角色: ===>"+corporationName+"-普通用户");
				map.put("JSID", jsid);
			}else{
				map.put("JSID", MapUtils.getInteger(roleMap, "DID"));
			}
			insertDept(map);
		}
		
	}
	
	
	/**
	 * 循环添加部门 和用户
	 * @param pkdept
	 */
	private void insertDept(Map parentMap){
		//当前部门的主键
		String pkdept = MapUtils.getString(parentMap, "PK_DEPT");
		List<Map> deptList = portalMapper.getDeptByCorporation(pkdept);
		if(deptList!=null&&deptList.size()>0){
			for (Map map : deptList) {
				String groupName = MapUtils.getString(map, "DEPTNAME");
				map.put("QZH", MapUtils.getString(parentMap, "QZH"));
				map.put("JSID", MapUtils.getInteger(parentMap, "JSID"));
				if(MapUtils.getInteger(parentMap, "PID")!=null){//如果上级是部门不是全宗
					map.put("PID", MapUtils.getInteger(parentMap, "DID"));
				}else{
					map.put("PID", 0);
				}
				map.put("GNAME", groupName);
				Map thamsGourp = thamsMapper.getGroupByBz(MapUtils.getString(map, "PK_DEPT"));
				if(thamsGourp==null){//如果不存在当前部门
					//执行添加子部门
					Integer did = thamsMapper.getMaxDIDForGroup();
					did = did!=null?did+1:1;
					map.put("DID", did);
					map.put("GID", did);
					map.put("BZ", MapUtils.getString(map, "PK_DEPT"));
					thamsMapper.insertGroup(map);
					log.info("添加部门: ===>"+groupName);
				}else{//如果存在
					map.put("DID", MapUtils.getInteger(thamsGourp, "DID"));
					thamsMapper.updateGroupQzh(map);
					log.info("部门: ===>"+groupName);
				}
				//添加当前部门下用户
				insertUser(map);
				//添加子部门
				insertDept(map);
			}
		}
	}
	
	
	/**
	 * 添加部门下得用户
	 * @param map 部门对象
	 */
	private void insertUser(Map map){
		List<Map> userList = portalMapper.getUserList(MapUtils.getString(map, "PK_DEPT"));
		for (Map portalUser : userList) {
			String usercode = MapUtils.getString(portalUser, "USERID");
			Integer groupID = MapUtils.getInteger(map, "DID");
			String portalUserName = MapUtils.getString(portalUser, "USERNAME");
			Encode ncencode = new Encode();
			String portalPasswd = ncencode.decode(MapUtils.getString(portalUser, "PASSWORD"));
			portalUser.put("USERCODE", usercode);
			portalUser.put("USERNAME", portalUserName);
			portalUser.put("PASSWD", portalPasswd);
			Map thamsUser = thamsMapper.getUserByUsercode(usercode);
			if(thamsUser==null){//如果不存在当前用户 则 添加
				Integer did = thamsMapper.getMaxDIDForUser();
				did = did!=null?did+1:1;
				portalUser.put("DID", did);
				portalUser.put("PID", groupID);
				thamsMapper.insertUser(portalUser);
				log.info("添加用户: ===>"+portalUserName);
				
				Map userRoleMap = new HashMap();
				Integer userRoleDID = thamsMapper.getMaxDIDForUserRole();
				userRoleDID = userRoleDID!=null?userRoleDID+1:1;
				userRoleMap.put("DID", userRoleDID);
				userRoleMap.put("YHID", did);
				userRoleMap.put("JSID", MapUtils.getInteger(map, "JSID"));
				thamsMapper.insertUserRole(userRoleMap);
			}else if(!MapUtils.getString(thamsUser, "USERNAME").equals(portalUserName)||!MapUtils.getString(thamsUser, "PASSWD").equals(portalPasswd)){
				thamsMapper.updateUser(portalUser);
				log.info("修改用户: ===>"+portalUserName);
			}else{
				thamsMapper.updateUserDept(groupID, usercode);
			}
		}
	}
	
	
	
	private String  fillZero(String qzh){
		while(qzh.length()<4){
			qzh = "0"+qzh;
		}
		return qzh;
	}
	
}
